package com.phone580.app.apphome;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.phone580.app.R;
import com.phone580.app.entity.AppInfoEntity;
import com.phone580.app.untilty.CommonUtility;

public class AppOnPhoneAdapter extends BaseAdapter{
	private LayoutInflater inflater;
	private List<AppInfoEntity> data;
	private Context context;
	private BitmapDrawable icon;
	
	public AppOnPhoneAdapter (Context context) {
		this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.context = context;
		Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.to_sd_icon);
		icon = new BitmapDrawable(bitmap);
	}

	public void setData(List<AppInfoEntity> data) {
		this.data = data;
	}
	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public AppInfoEntity getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		ViewHolder holder = null;
		if (v == null) {
			v = inflater.inflate(R.layout.app_info_item, null);
			holder = new ViewHolder();
			holder.ivIcon = (ImageView)v.findViewById(R.id.app_info_icon_iv);
			holder.tvTitle = (TextView) v.findViewById(R.id.app_info_title_tv);
			holder.tvSize = (TextView) v.findViewById(R.id.app_info_size_tv);
			holder.tvMove = (TextView) v.findViewById(R.id.app_info_move_tv);
			holder.tvMove.setText("到SD卡");
			
			holder.tvMove.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.to_sd_icon, 0, 0);
//			holder.tvMove.setCompoundDrawables(null,icon, null, null);
			v.setTag(holder);
		}
		holder = (ViewHolder) v.getTag();
		final AppInfoEntity entity = data.get(position);
		holder.ivIcon.setImageDrawable(entity.getIcon());
		holder.tvTitle.setText(entity.getAppLabel());
		holder.tvSize.setText(entity.getSize() / 1024l + "kb");
		holder.tvMove.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				CommonUtility.showInstalledAppDetails(context, entity.getPkgName());
			}
		});
		return v;
	}
	
	class ViewHolder {
		public ImageView ivIcon;
		public TextView tvTitle;
		public TextView tvSize;
		public TextView tvMove;
	}

}
